//
//  HBReadSubController.m
//  HeartBeat
//
//  Created by karong on 2023/9/12.
//

#import "HBReadSubController.h"
#import "HBReadSettingManager.h"
#import "HBReadView.h"

@interface HBReadSubController ()

@end

@implementation HBReadSubController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    [self loadData];
    [self.readView setContent:[self attContent]];
    [self setModel:self.model chapter:self.chapter pageIndex:self.pageIndex];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if ([self.delegate respondsToSelector:@selector(viewDidAppear:animated:)]) {
        [self.delegate viewDidAppear:self animated:animated];
    }
}

- (void)setModel:(HBChapterModel *)model chapter:(NSInteger)chapter pageIndex:(NSInteger)pageIndex {
    self.model = model;
    self.chapter = chapter;
    self.pageIndex = pageIndex;
    self.readView.content = [self.model getContentAtt:pageIndex];
    NSInteger totalPage = self.model.pageCount;
    pageIndex = pageIndex + 1 >totalPage ? totalPage : pageIndex + 1;
    self.selectLab.text = [NSString stringWithFormat:@"%@/%@",@(pageIndex),@(totalPage)];
    self.titleLab.text = self.model.title ?:@"";
    self.selectLab.textColor = [UIColor colorWithHexString:[[HBReadSettingManager shared] showModel].colorStr];
    self.titleLab.textColor = [UIColor colorWithHexString:[[HBReadSettingManager shared] showModel].colorStr];
    [self loadData];
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
}

- (void)loadData{
    self.view.backgroundColor = [[HBReadSettingManager shared] showSkinColor];
}

- (BOOL)landscape{
    UIInterfaceOrientation state= [UIApplication sharedApplication].statusBarOrientation;
    return state == UIInterfaceOrientationLandscapeLeft || state == UIInterfaceOrientationLandscapeRight;
}

#pragma mark base
- (BOOL)shouldAutorotate{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return  UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator{
    
}

- (NSAttributedString *)attContent{
    NSAttributedString *att = [[NSAttributedString alloc]initWithString:WSLocalized(@"加载中...") attributes:[self defaultFont]];
    return att;
}
- (NSDictionary *)defaultFont{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    paragraphStyle.allowsDefaultTighteningForTruncation = YES;
    NSDictionary *dic =  @{NSForegroundColorAttributeName:UIColor.font_white_ffffff,
                           NSFontAttributeName:[UIFont systemFontOfSize:18],
                           NSParagraphStyleAttributeName:paragraphStyle
                           };
    return dic;
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
    if ([self.delegate respondsToSelector:@selector(tapAction:)]) {
        [self.delegate tapAction:sender];
    }
}
@end
